Reveal Your Intent
use intent-revealing name, if your need a comment ,that's not a good name.
-
bad:
1int d; // elapsed time in days -
good
12345678910111213int elapsedTimeInDays;```# Descpribe Your Problem* bad:```java/***Useful range constant*/public static final int INLCUDE_NONE=0;/***Useful range constant*/public static final int INLCUDE_NONE=1;/***Useful range constant*/public static final int INLCUDE_NONE=2;/***Useful range constant*/public static final int INLCUDE_NONE=3;
can you tell me what they mean,did the comment helpful? 'userful?' are their useless code?'range?'range of what?'Constant' ofco use its a constant
whenever you have to read the code in order to understand the name, the name has pretty much failed to communicate the intent.It's a bad name. Remember names are not for your convienient, it's your primariy tools for communicate intent.Commuicate your intent is always your first priority,it's even more important than the code work.
Avoid Disinformation
- disinformation: name in code does not mean what it's said.(worsest sins) a misleading name can cous
Pronounceable Names
- bad
123456789int PC_GWDA;public int getYYYY(){return this.year;}int qty_tests =0;int qty_pass_m=0;int qty_pass_s =0;int qty_skip = 0;int qty_fails=0;
Avoid Encoding
prifix: p mean point,c mean char, b mean boolean.C for Class,I for Interface. C: Class CAccount I:Interface IAccount p: pointer pAccount s: String sName st: Null Terminated StName
now days,IDE can tell all this informations,i won't be using them.That' 1990s legency,silly prifixs.Just use names! Let the IDE,compiller and test do the rest. 'Account is better than IAccount'
Part of Speech
class and variable are norns or norn pharse,methods are verbs. ignore silly nosie words.
-
good ACOUUNT,MESSAGEPARSER
-
silly nosie words MANAGER,DATA,INFO,PROSSOR
-
boolean variabels should been write like predicats,they read well,like
isEmpty
,isTerminated
-
methods should been verbs or verbs pharese, they read well,like
getPirce
,postPayment
-
if a methods returns a boolean,then should be predicats,beacuse they likely be used in if statement and they read well,like
isPostable
12345678boolean isEmpty;boolean isTerminated;---if(isEmpty){if (payment.isPostable()){postPayment(payment);}} -
Enums tends to be states or object descriptors,so they often adj.
123enum Color{RED,GREEN,BLUE};enum Status{PENDING,CLOSED,CANCELLED};enum Size{SMALL,MEDIUM,LARGE};
remmeber: it's a lot easier to read code if the statements form sentences that read like well written poems.
1 if(employee.isLate() employee.reprimand();
The Scope of Length Rule
- for varibales:
the longger the scope of variable,the longger veriable names,but the variable with short scope should have short names;
here
tr
is a good name,butd
is bad.element
should be shorten toe
.
- for funtions(oppsite):
the longer the scope,the shorter the function or name should be;
the shortter the scope,the longer the function should be.
we like long socpe public function names short like
serve
,convinient to use,long name hard to tell apart;we like short scope private function names long liketryProcessInstructions
,closeEnclosingServiceInSeperateThread
,only called from here ,it's kind of a comment, explain themselves. The same arguement can be applied to Classes.